<template>
    <el-dialog :title="editOrAdd=='edit' ? '编辑医院' : '新增医院'" :visible="dialogEdit" @close="close">
        <el-form ref="form" label-width="100px" :rules="rules" :model="form">
            <el-form-item label="医院名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入医院名称"></el-input>
            </el-form-item>
            <el-form-item label="所属城市" prop="city">
                <el-select clearable class="filter-item" placeholder="请选择所在城市" v-model="form.city" style="width: 100%;">
                    <el-option v-for="(item, index) in cityOptions" :key="item.name" :label="item.name" :value="item.name"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="onSubmit('form')">保存</el-button>
            <el-button plain @click="close">取消</el-button>
        </span>
    </el-dialog>
</template>

<script>
  import citys from '@/utils/city'
  import { fetchHospital, fetchHospitalUpdate, fetchHospitalAdd } from '@/api/hospital'

  export default {
    name: 'HospitalManage',
    props: ['hid', 'editOrAdd', 'dialogEdit'],
    data() {
      return {
        defaultForm: {
          name: '',
          city: ''
        },
        form: {
          name: '',
          city: ''
        },
        isEdit: false,
        cityOptions: citys,
        rules: {
          name: [
            { required: true, message: '请输入医院名称', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '请选择城市', trigger: 'blur' }
          ]
        }
      }
    },
    created: function() {
      this.form = this.defaultForm
    },
    watch: {
      dialogEdit(val) {
        if (val) {
          this.initForm()
        }
      }
    },
    methods: {
      initForm() {
        if (this.editOrAdd === 'add') {
          this.isEdit = false
          this.form = this.defaultForm
          this.$nextTick(() => {
            this.$refs['form'] && this.$refs['form'].resetFields()
          })
        } else {
          this.isEdit = true
          this.getInfo()
          this.$nextTick(() => {
            this.$refs['form'] && this.$refs['form'].clearValidate()
          })
        }
      },
      getInfo() {
        fetchHospital({ id: this.hid }).then(response => {
          if (response) {
            this.form = response.data
          }
        })
      },
      close() {
        this.$emit('update:dialogEdit', false)
      },
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.isEdit) {
              fetchHospitalUpdate(this.form).then(response => {
                if (response) {
                  this.$message({ message: '修改医院成功', type: 'success', showClose: true })
                  this.$emit('update:dialogEdit', false)
                  this.$emit('refreshList')
                }
              })
            } else {
              this.form.region = this.region
              fetchHospitalAdd(this.form).then(response => {
                if (response) {
                  this.$message({ message: '添加医院成功', type: 'success', showClose: true })
                  this.$emit('update:dialogEdit', false)
                  this.$emit('refreshList')
                }
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>
